package com.example.administrator.damaiwang;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.util.SparseArray;
import android.view.Window;
import android.widget.RadioGroup;

import com.example.administrator.damaiwang.fragmentpack.FindFragment;
import com.example.administrator.damaiwang.fragmentpack.MeFragment;
import com.example.administrator.damaiwang.fragmentpack.TuiJianFragment;
import com.example.administrator.damaiwang.fragmentpack.YanChuFragment;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener{
    private RadioGroup mRg;
    private SparseArray<Fragment> mFragments = new SparseArray<>();
    private FragmentManager mFragmentManager;
    private Fragment mCurrentFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
//        mVp = (ViewPager) findViewById(R.id.vp);
        mRg = (RadioGroup) findViewById(R.id.rdg_navigation);
        initFragment();
        initView();

        //       mVp.setAdapter(new MyAdapter(getSupportFragmentManager()));
        //通过ViewPager的切换,来实现底部导航栏的切换.
//        mVp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
//
//            @Override
//            public void onPageSelected(int position) {
//                super.onPageSelected(position);
//                ((RadioButton) mRg.getChildAt(position)).setChecked(true);
//            }
//        });
        //通过底部导航栏的切换,来实现ViewPager的切换.
//        for (int i = 0; i <mRg.getChildCount() ; i++) {
//            //存值
//            mRg.getChildAt(i).setTag(i);
//            mRg.getChildAt(i).setOnClickListener(this);
//
//        }

    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        switch (checkedId) {
            case R.id.rd_tuijian:
                switchFragment(mFragments.get(0));
                break;
            case R.id.rd_yanchang:
                switchFragment(mFragments.get(1));
                break;
            case R.id.rd_find:
                switchFragment(mFragments.get(2));
                break;
            case R.id.rd_me:
                switchFragment(mFragments.get(3));
                break;
        }

    }
    private void switchFragment(Fragment toFragment) {
        if (toFragment.isAdded()) {
            mFragmentManager.beginTransaction()
                    .hide(mCurrentFragment)
                    .show(toFragment)
                    .commit();
        } else {
            mFragmentManager.beginTransaction()
                    .hide(mCurrentFragment)
                    .add(R.id.lv_main, toFragment)
                    .commit();
        }
        mCurrentFragment = toFragment;
    }
    private void initFragment() {
       Fragment tuiJianFragment= new TuiJianFragment();
        Fragment yanChuFragment = new YanChuFragment();
        Fragment findFragment = new FindFragment();
        Fragment  meFragment = new MeFragment();

        mFragments.put(0, tuiJianFragment);
        mFragments.put(1, yanChuFragment);
        mFragments.put(2, findFragment);
        mFragments.put(3, meFragment);
    }
    private void initView() {
        mRg.setOnCheckedChangeListener(this);
        mFragmentManager = getSupportFragmentManager();
        mFragmentManager.beginTransaction().add(R.id.lv_main, mFragments.get(0)).commit();
        mCurrentFragment = mFragments.get(0);
    }
//    @Override
//    public void onClick(View view) {
//        mVp.setCurrentItem((int) view.getTag(),false);
//    }
//
//    class MyAdapter extends FragmentPagerAdapter {
//
//
//        public MyAdapter(FragmentManager fm) {
//            super(fm);
//        }
//
//        @Override
//        public Fragment getItem(int position) {
//            return FragmentFactory.createFragment(position);
//        }
//
//        @Override
//        public int getCount() {
//            return mRg.getChildCount();
//        }
//    }

}
